clear all
set more off , perm
capture log close
set type double

********************************************************************************
***************** This file calculates shortest commuter paths *****************
********************************************************************************

	
**********************************************************
****   Prepare data: station to station distance      ****
**********************************************************	
	
// Load data
cd "$dta"
use french_stations_coordinates.dta, clear

//	Drop stations without lat long info	
drop if latitude == . | longitude ==.

//	Merge stations with others within 10 km
rename id_pdv id_master
rename latitude latitude_master
rename longitude longitude_master

geonear id_master latitude_master longitude_master ///
		using "$dta\french_stations_coordinates.dta", ///
		neighbors(id_pdv latitude longitude) ///
		long within(10) ignoreself 
drop if km_to_id_pdv > 10		
merge m:1 id_pdv using "$dta\french_stations_coordinates.dta", ///
		keep(match master) keepusing(latitude longitude)
	drop _merge
rename id_pdv id_using
rename latitude latitude_using
rename longitude longitude_using

rename km_to_id_pdv flight_distance

rename id_master id_pdv 
merge m:1 id_pdv using "$dta\french_stations_coordinates.dta", ///
		keep(match master) keepusing(latitude longitude)
	drop _merge

rename latitude latitude_master
rename longitude longitude_master
rename id_pdv id_master 


// Calculate shortest path

** only keep one pair
egen double a = group(id_master id_using)
egen double b = group(id_using id_master)
egen double c = rowmin(a b)
egen double dyad = group(c)
bysort dyad: gen dyad_n = _n
bysort dyad: gen dyad_N = _N
bysort id_master: gen id_master_N = _N

keep if dyad_n == 1


timer clear 1
timer on 1

osrmtime latitude_master longitude_master latitude_using longitude_using , mapfile("$raw\09_France_osrm_map\france-latest.osrm") nocleanup



// Save data
compress
cd "$dta"
save distance_france_stations_oneway.dta , replace
timer off 1
timer list 1

keep id_master id_using distance jumpdist1 jumpdist2

tempfile aux_1
save `aux_1' , replace

use `aux_1' , clear
rename id_master aux
rename id_using id_master
rename aux id_using
append using `aux_1'
save distance_france_stations.dta , replace


	
	
********************************************************
****   Prepare data: 1, 3, 5 km station market      ****
********************************************************	

// Create a data set with the market focal station only
use "$dta\french_stations_coordinates.dta" , clear
keep id_pdv
tostring id_pdv , replace
gen using_id_data = id_pdv
rename id_pdv master_id_data
gen tot_distance_s_s = 0
cd "$dta"
save focal_station.dta , replace

// Create a data set with IDs of all stations that have SP95, E10 or diesel in 2013-2014 period (drop all other stations since these are neither local monopolists, nor competitors)
use "$raw\04_France_prices\PrixCarburants_annuel_2013.dta" , clear
append using "$raw\04_France_prices\PrixCarburants_annuel_2014.dta"

	*Drop all prices but SP95 and E10
	keep if (id_prix==2 & nom=="SP95") | (id_prix == 5 & nom=="E10") | (id_prix == 1 & nom=="Gazole")
	drop nom id_prix
		
	* Drop stations on the motorway
	drop if pop == "A"
	drop pop
	
	bysort id_pdv: keep if _n == 1
	keep id_pdv
	rename id_pdv master_id_data
	tostring master_id_data , replace
	cd "$dta"
	save relevant_stations_m.dta , replace
	rename master_id_data using_id_data
	save relevant_stations_u.dta , replace
	
	
// Load data
use "$dta\distance_france_stations.dta" , clear

//  Append other data
gen tot_distance_s_s = distance + jumpdist1 + jumpdist2

drop  distance jumpdist1 jumpdist2

//	Rename
rename  id_master master_id_data
rename  id_using using_id_data

tostring master_id_data , replace
tostring using_id_data , replace


//	Save raw market data
compress
cd "$dta"
append using focal_station.dta
order master_id_data using_id_data
sort master_id_data using_id_data

*Delete stations that do not sell SP95, E10 or diesel in 2013-2014
merge m:1 master_id_data using relevant_stations_m.dta
keep if _m == 3
drop _merge
merge m:1 using_id_data using relevant_stations_u.dta
keep if _m == 3
drop _merge

// Save 5 km market
foreach i in 1 3 5 {
	preserve
	keep if tot_distance_s_s <= `i'*1000
	compress
	save station_markets_`i'_france.dta , replace
	restore
}

	
	
// Erase data 
erase french_stations_coordinates.dta 	
erase focal_station.dta
erase relevant_stations_m.dta
erase relevant_stations_u.dta
erase distance_france_stations.dta
